Skip to content

Commit

Permalink
fix(webpack-loader): compile from passed source text when file not found
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad0337187 committed Aug 24, 2019
1 parent 3a55df2 commit d9cf296
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions webpack-loader.coffee
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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?
Expand All @@ -28,15 +37,22 @@ 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
cache: true # CaffeineMc's external-reference-smart caching
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
Expand All @@ -48,4 +64,4 @@ module.exports = (source) ->
log.error "CaffeineMc webpack-loader error": e
throw e

module.exports.separable = true
module.exports.separable = true

0 comments on commit d9cf296

Please sign in to comment.