From 9001cee2c3709c6b0b351fceebf20a3e76199063 Mon Sep 17 00:00:00 2001 From: vlad1777d Date: Sun, 25 Aug 2019 01:13:37 +0300 Subject: [PATCH] fix(webpack-loader): compile from passed source text when file not found --- webpack-loader.coffee | 26 +++++++++++++++++++++----- webpack-loader.js | 40 +++++++++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/webpack-loader.coffee b/webpack-loader.coffee index c5d5caf..168965e 100644 --- a/webpack-loader.coffee +++ b/webpack-loader.coffee @@ -1,7 +1,10 @@ +fs = require 'fs' + +{log, getEnv} = require 'art-standard-lib' +loaderUtils = require 'loader-utils' + CaffeineMc = require './index' CaffeineEight = require 'caffeine-eight' -loaderUtils = require 'loader-utils' -{log, getEnv} = require 'art-standard-lib' ### TODO: Fix SOURCEMAPS (SBD 2018-07-30 notes) @@ -18,6 +21,12 @@ SO - cafSourceMaps is off by default, but you can turn it on if you want: {cafSourceMaps} = getEnv module.exports = (source) -> + ### + Compile source file if it's present on file system. + If it's inline source or just string compilation - than compile source itself. + (for example, when using inline code in .vue files) + ### + @cacheable?() # CaffeineMc manages its own cachability, but I'm unclear what disabling webpack's caching # does... Does it cache across runs? What triggers a re-load if cacheable is false? @@ -28,8 +37,9 @@ module.exports = (source) -> # Even with addDependency, it wouldn't catch a file being added which alters module-resolution. sourceFile = loaderUtils.getRemainingRequest @ - try - {compiled:{js, sourceMap}} = CaffeineMc.FileCompiler.compileFileSync sourceFile, { + fileExists = fs.existsSync(sourceFile) + + compileOptions = { source @debug sourceRoot: "" # make sourceMaps references relative to webpack's start directory @@ -37,6 +47,12 @@ module.exports = (source) -> inlineMap: !!cafSourceMaps # experimental - works in Safari, not Chrome prettier: !cafSourceMaps # prettier is incompatible with sourceMaps } + + try + if fileExists + {compiled: {js, sourceMap}} = CaffeineMc.FileCompiler.compileFileSync sourceFile, compileOptions + else + {compiled: {js, sourceMap}} = CaffeineMc.compile source, compileOptions @callback null, js, sourceMap catch e @@ -48,4 +64,4 @@ module.exports = (source) -> log.error "CaffeineMc webpack-loader error": e throw e -module.exports.separable = true \ No newline at end of file +module.exports.separable = true diff --git a/webpack-loader.js b/webpack-loader.js index 821d63e..64182ee 100644 --- a/webpack-loader.js +++ b/webpack-loader.js @@ -1,14 +1,16 @@ // Generated by CoffeeScript 1.12.7 (function() { - var CaffeineEight, CaffeineMc, cafSourceMaps, getEnv, loaderUtils, log, ref; + var CaffeineEight, CaffeineMc, cafSourceMaps, fs, getEnv, loaderUtils, log, ref; - CaffeineMc = require('./index'); + fs = require('fs'); - CaffeineEight = require('caffeine-eight'); + ref = require('art-standard-lib'), log = ref.log, getEnv = ref.getEnv; loaderUtils = require('loader-utils'); - ref = require('art-standard-lib'), log = ref.log, getEnv = ref.getEnv; + CaffeineMc = require('./index'); + + CaffeineEight = require('caffeine-eight'); /* @@ -26,20 +28,32 @@ cafSourceMaps = getEnv.cafSourceMaps; module.exports = function(source) { - var e, js, out, ref1, sourceFile, sourceMap; + + /* + Compile source file if it's present on file system. + If it's inline source or just string compilation - than compile source itself. + (for example, when using inline code in .vue files) + */ + var compileOptions, e, fileExists, js, out, ref1, ref2, sourceFile, sourceMap; if (typeof this.cacheable === "function") { this.cacheable(); } sourceFile = loaderUtils.getRemainingRequest(this); + fileExists = fs.existsSync(sourceFile); + compileOptions = { + source: source, + debug: this.debug, + sourceRoot: "", + cache: true, + inlineMap: !!cafSourceMaps, + prettier: !cafSourceMaps + }; try { - ref1 = CaffeineMc.FileCompiler.compileFileSync(sourceFile, { - source: source, - debug: this.debug, - sourceRoot: "", - cache: true, - inlineMap: !!cafSourceMaps, - prettier: !cafSourceMaps - }).compiled, js = ref1.js, sourceMap = ref1.sourceMap; + if (fileExists) { + ref1 = CaffeineMc.FileCompiler.compileFileSync(sourceFile, compileOptions).compiled, js = ref1.js, sourceMap = ref1.sourceMap; + } else { + ref2 = CaffeineMc.compile(source, compileOptions).compiled, js = ref2.js, sourceMap = ref2.sourceMap; + } return this.callback(null, js, sourceMap); } catch (error) { e = error;