gulp-requirejs-optimize icon indicating copy to clipboard operation
gulp-requirejs-optimize copied to clipboard

Options to include path in module name

Open johannes-z opened this issue 9 years ago • 4 comments

I want to optimize some modules with the requirejs optimizer, but ran into a problem concerning paths. The main.js entry point requires the component on /build/modules/, but the name of the optimized module does not match this path. The component file is loaded, but the function it defines isn't called. main_component.js requires the components moduleA and moduleB, and the build's main.js requires the built main_component.js.

Code of ./build/main.js:

require(['./modules/main_component'], function () {
  console.log('main')
})

Code of ./build/modules/main_component:

/* component definitions ... */
define('main_component', // <- this needs to be 'modules/main_component' 
	['require','components/moduleA','components/moduleB'],
	function (require) { /* ... */}
)

Project structure:

/
|-- build
|    |-- modules
|    |   +-- main_component.js # optimized file
|    +-- main.js
|-- js
|    +-- modules
|        |-- components
|        |    |-- moduleA.js
|        |    +-- moduleB.js
|        +-- main_component.js
+-- index.html

Also, I'm using a gulp wrapper gulp-requirejs-optimize. This is my gulpfile:

var gulp = require('gulp')
var requirejsOptimize = require('gulp-requirejs-optimize')

gulp.task('default', function () {
  return gulp.src('js/modules/*.js')
	.pipe(requirejsOptimize({
	  optimize: 'none'
	}))
	.pipe(gulp.dest('build/modules'))
})

This is also a stackoverflow question: http://stackoverflow.com/questions/40742457/requirejs-optimizer-how-to-change-the-optimized-module-name

johannes-z avatar Nov 22 '16 14:11 johannes-z

Ok, looks like skipDirOptimize: true does excatly what I wanted, but seems I've missed this option until now. Thanks anyway!

johannes-z avatar Nov 22 '16 14:11 johannes-z

This seems to work only though if the src is set to js/**/*.js, and then the components files are also optimized and put into the build dir, although they aren't needed there.

johannes-z avatar Nov 22 '16 14:11 johannes-z

Also, this seems to work for the path:

gulp.task('default', function () {
  return gulp.src('./js/modules/*.js')
    .pipe(requirejsOptimize(function (file) {
      return {
        optimize: 'none',
        baseUrl: 'js',
        include: 'modules/' + file.relative
      }
    }))
    .pipe(gulp.dest('build/modules'))
})

But this generates the name as follows, where the file extension is a problem. Also, I couldn't change file.relative to file.stem, because it is undefined.

define('modules/main_component.js', ...

johannes-z avatar Nov 22 '16 15:11 johannes-z

Can you just remove the .js extension from file.relative by slicing the string?

jlouns avatar Mar 26 '17 22:03 jlouns