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