]> git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/index.js
Support for PureScript 0.11
[github/fretlink/purs-loader.git] / src / index.js
1 'use strict'
2
3 const debug = require('debug')('purs-loader')
4
5 const loaderUtils = require('loader-utils')
6
7 const Promise = require('bluebird')
8
9 const path = require('path')
10
11 const PsModuleMap = require('./purs-module-map');
12
13 const compile = require('./compile');
14
15 const bundle = require('./bundle');
16
17 const ide = require('./ide');
18
19 const toJavaScript = require('./to-javascript');
20
21 const dargs = require('./dargs');
22
23 const spawn = require('cross-spawn').sync
24
25 const eol = require('os').EOL
26
27 module.exports = function purescriptLoader(source, map) {
28 const callback = this.async()
29 const config = this.options
30 const query = loaderUtils.getOptions(this) || {}
31 const webpackOptions = this.options.purescriptLoader || {}
32
33 const depsPaths = (pscPackage => {
34 if (pscPackage) {
35 debug('calling psc-package...')
36
37 return spawn('psc-package', ['sources']).stdout.toString().split(eol).filter(v => v != '')
38 }
39 else {
40 return [ path.join('bower_components', 'purescript-*', 'src', '**', '*.purs') ]
41 }
42 })
43
44 let options = Object.assign(webpackOptions, query)
45
46 const defaultDeps = depsPaths(options.pscPackage)
47 const defaultOptions = {
48 context: config.context,
49 psc: null,
50 pscArgs: {},
51 pscBundle: null,
52 pscBundleArgs: {},
53 pscIde: false,
54 pscIdeColors: options.psc === 'psa',
55 pscIdeArgs: {},
56 pscPackage: false,
57 bundleOutput: 'output/bundle.js',
58 bundleNamespace: 'PS',
59 bundle: false,
60 warnings: true,
61 watch: false,
62 output: 'output',
63 src: [
64 path.join('src', '**', '*.purs'),
65 ...defaultDeps
66 ]
67 }
68
69 this.cacheable && this.cacheable()
70
71 let cache = config.purescriptLoaderCache = config.purescriptLoaderCache || {
72 rebuild: false,
73 deferred: [],
74 bundleModules: [],
75 warnings: [],
76 errors: []
77 }
78
79 if (options.pscPackage && options.src) {
80 options.src = options.src.concat(defaultDeps) // append psc-package-provided source paths with users'
81 }
82
83 options = Object.assign(defaultOptions, options)
84
85 if (!config.purescriptLoaderInstalled) {
86 config.purescriptLoaderInstalled = true
87
88 // invalidate loader cache when bundle is marked as invalid (in watch mode)
89 this._compiler.plugin('invalid', () => {
90 debug('invalidating loader cache');
91
92 cache = config.purescriptLoaderCache = {
93 rebuild: options.pscIde,
94 deferred: [],
95 bundleModules: [],
96 ideServer: cache.ideServer,
97 psModuleMap: cache.psModuleMap,
98 warnings: [],
99 errors: []
100 }
101 });
102
103 // add psc warnings to webpack compilation warnings
104 this._compiler.plugin('after-compile', (compilation, callback) => {
105 cache.warnings.forEach(warning => {
106 compilation.warnings.push(warning);
107 });
108
109 cache.errors.forEach(error => {
110 compilation.errors.push(error);
111 });
112
113 callback()
114 });
115 }
116
117 const psModuleName = PsModuleMap.matchModule(source)
118 const psModule = {
119 name: psModuleName,
120 load: js => callback(null, js),
121 reject: error => callback(error),
122 srcPath: this.resourcePath,
123 srcDir: path.dirname(this.resourcePath),
124 jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')),
125 options: options,
126 cache: cache,
127 emitWarning: warning => {
128 if (options.warnings && warning.length) {
129 cache.warnings.push(warning);
130 }
131 },
132 emitError: error => {
133 if (error.length) {
134 cache.errors.push(error);
135 }
136 }
137 }
138
139 debug('loader called', psModule.name)
140
141 if (options.bundle) {
142 cache.bundleModules.push(psModule.name)
143 }
144
145 if (cache.rebuild) {
146 return ide.connect(psModule)
147 .then(ide.rebuild)
148 .then(toJavaScript)
149 .then(psModule.load)
150 .catch(psModule.reject)
151 }
152
153 if (cache.compilationFinished) {
154 return toJavaScript(psModule).then(psModule.load).catch(psModule.reject)
155 }
156
157 // We need to wait for compilation to finish before the loaders run so that
158 // references to compiled output are valid.
159 cache.deferred.push(psModule)
160
161 if (!cache.compilationStarted) {
162 cache.compilationStarted = true;
163
164 return compile(psModule)
165 .then(() => {
166 cache.compilationFinished = true;
167
168 const bundlePromise = options.bundle ? bundle(options, cache) : Promise.resolve();
169
170 return bundlePromise.then(() =>
171 PsModuleMap.makeMap(options.src).then(map => {
172 debug('rebuilt module map after compile');
173 cache.psModuleMap = map;
174 })
175 );
176 })
177 .then(() => Promise.map(cache.deferred, psModule => {
178 if (typeof cache.ideServer === 'object') cache.ideServer.kill()
179 return toJavaScript(psModule).then(psModule.load)
180 }))
181 .catch(error => {
182 cache.deferred[0].reject(error)
183 cache.deferred.slice(1).forEach(psModule => psModule.reject(new Error('purs-loader failed')))
184 })
185 }
186 }